package cn.edu.cug.cs.gtl.gsm.jdbc.postgres;

import cn.edu.cug.cs.gtl.common.Identifier;
import cn.edu.cug.cs.gtl.gsm.item.IndexItem;
import cn.edu.cug.cs.gtl.util.StringUtils;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class IndexTable {
    private String INDEX_TABLE_NAME;
    private String SQL_DROP_INDEX_TABLE;
    private String SQL_CREATE_INDEX_TABLE;
    private String SQL_DELETE_INDEX_TABLE_DATA;
    private String SQL_SELECT_INDEX_TABLE_DATA;
    private String SQL_SELECT_ALL_INDEX_TABLE_DATA;
    private String SQL_DELETE_ALL_INDEX_TABLE_DATA;
    private String SQL_INSERT_INDEX_TABLE_DATA;
    private String SQL_UPDATE_INDEX_TABLE_DATA;

    protected Connection connection;


    public IndexTable(Connection connection,String baseName) {
        this.connection = connection;
        INDEX_TABLE_NAME=baseName+".rdb_sm_index_"+baseName;
        SQL_DROP_INDEX_TABLE= "DROP TABLE IF EXISTS "+INDEX_TABLE_NAME;
        SQL_CREATE_INDEX_TABLE= "CREATE TABLE " + INDEX_TABLE_NAME + " ( page_identifier  VARCHAR(255) PRIMARY KEY, index_item VARCHAR)";
        SQL_DELETE_INDEX_TABLE_DATA= "DELETE FROM " + INDEX_TABLE_NAME + " WHERE page_identifier = ?";
        SQL_SELECT_INDEX_TABLE_DATA= "SELECT index_item FROM " + INDEX_TABLE_NAME + " WHERE page_identifier = ?";
        SQL_SELECT_ALL_INDEX_TABLE_DATA= "SELECT index_item FROM " + INDEX_TABLE_NAME ;
        SQL_DELETE_ALL_INDEX_TABLE_DATA= "DELETE FROM " + INDEX_TABLE_NAME ;
        SQL_INSERT_INDEX_TABLE_DATA= "INSERT INTO " + INDEX_TABLE_NAME + " VALUES (?,?)";
        SQL_UPDATE_INDEX_TABLE_DATA= "UPDATE " + INDEX_TABLE_NAME + " SET index_item = ? WHERE page_identifier=?";
    }


    public boolean create() throws SQLException{
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_CREATE_INDEX_TABLE);
        return r;
    }

    public boolean drop() throws SQLException {
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_DROP_INDEX_TABLE);
        return r;
    }

    public boolean delete(Identifier pageIdentifier) throws SQLException{
        PreparedStatement statement = connection.prepareStatement(SQL_DELETE_INDEX_TABLE_DATA);
        statement.setString(1,pageIdentifier.toString());
        boolean r =  statement.execute();
        return r;
    }

    public boolean deleteAll() throws SQLException{
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_DELETE_ALL_INDEX_TABLE_DATA);
        return r;
    }

    public boolean insert(IndexItem item) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_INSERT_INDEX_TABLE_DATA);
        statement.setString(1,item.getExternalPageIdentifier().toString());
        statement.setString(2,item.storeToCompressString());
        boolean r =  statement.execute();
        return r;
    }

    public boolean update(IndexItem item) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_UPDATE_INDEX_TABLE_DATA);
        statement.setString(1,item.storeToCompressString());
        statement.setString(2,item.getExternalPageIdentifier().toString());
        boolean r =  statement.execute();
        return r;
    }

    public IndexItem select(Identifier externalPageIdentifier) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_SELECT_INDEX_TABLE_DATA);
        statement.setString(1,externalPageIdentifier.toString());
        ResultSet r =  statement.executeQuery();
        String compressZip = null;
        while(r.next()){
            compressZip = r.getString(1);
        }
        if(compressZip!=null) {
            IndexItem ids = new IndexItem();
            ids.loadFromCompressString(compressZip);
            return ids;
        }
        else
            return null;
    }

    public List<IndexItem> selectAll() throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_SELECT_ALL_INDEX_TABLE_DATA);
        ResultSet r =  statement.executeQuery();
        List<IndexItem> ls=new ArrayList<>();
        while(r.next()){

            String compressZip = null;
            compressZip = r.getString(1);
            if(compressZip!=null) {
                IndexItem ids = new IndexItem();
                ids.loadFromCompressString(compressZip);
                ls.add( ids);
            }
        }
        return ls;
    }

    public boolean exists() throws SQLException{
        String []sa = StringUtils.split(INDEX_TABLE_NAME,".");
        return PostgresStorageManager.exists(connection,sa[0],sa[1]);
    }

}
